package controllers

import (
	"encoding/json"
	beego "github.com/beego/beego/v2/server/web"
	"strconv"
	"time"
	"tour/models"
)

type TourControllers struct {
	beego.Controller
}

type Msg struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func (t *TourControllers) Add() {
	t.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	name := t.GetString("name")
	types := t.GetString("types")
	money, _ := t.GetFloat("money")
	if name == "" || types == "" || money == 0 {
		t.Data["json"] = Msg{
			Code: 0,
			Msg:  "不能为空",
			Data: nil,
		}
		t.ServeJSON()
		return
	}
	tour := models.Tour{
		Name:  name,
		Types: types,
		Money: money,
	}
	err := models.DB.Create(&tour).Error

	tour_redis_key := "tour_id" + strconv.Itoa(int(tour.ID))
	cacheJson, _ := json.Marshal(tour)
	models.Rdb.Set(models.Ctx, tour_redis_key, cacheJson, time.Hour*1)

	if err != nil {
		t.Data["json"] = Msg{
			Code: 0,
			Msg:  "添加失败",
			Data: nil,
		}
		t.ServeJSON()
		return
	} else {
		t.Data["json"] = Msg{
			Code: 200,
			Msg:  "添加成功",
			Data: tour,
		}
		t.ServeJSON()
	}
}

func (t *TourControllers) List() {
	t.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	page, _ := t.GetInt("page", 1)
	size, _ := t.GetInt("size", 3)
	offset := (page - 1) * size
	post := models.DB.Model(&models.Tour{})

	var count int64
	post.Count(&count)

	var tours []models.Tour

	tour_redis_key := "tour_list"
	cacheJson, _ := models.Rdb.Get(models.Ctx, tour_redis_key).Result()
	if cacheJson == "" {
		models.DB.Find(&tours)
		cacheJson, _ := json.Marshal(tours)
		models.Rdb.Set(models.Ctx, tour_redis_key, cacheJson, time.Hour*1)
	} else {
		json.Unmarshal([]byte(cacheJson), &tours)
	}

	err := post.Offset(offset).Limit(size).Find(&tours).Error
	if err != nil {
		t.Data["json"] = Msg{
			Code: 0,
			Msg:  "展示失败",
			Data: nil,
		}
		t.ServeJSON()
		return
	} else {
		postData := make(map[string]interface{})
		postData["page"] = page
		postData["size"] = size
		postData["sile"] = tours
		postData["sum"] = count
		t.Data["json"] = Msg{
			Code: 200,
			Msg:  "展示成功",
			Data: postData,
		}
		t.ServeJSON()
	}

}

func (t *TourControllers) Up() {
	t.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	id, _ := t.GetInt("id")
	name := t.GetString("name")
	types := t.GetString("types")
	money, _ := t.GetFloat("money")
	if name == "" || types == "" || money == 0 {
		t.Data["json"] = Msg{
			Code: 0,
			Msg:  "不能为空",
			Data: nil,
		}
		t.ServeJSON()
		return
	}
	tour := models.Tour{
		Name:  name,
		Types: types,
		Money: money,
	}
	err := models.DB.Model(&tour).Where("id=?", id).Updates(&tour).Error
	if err != nil {
		t.Data["json"] = Msg{
			Code: 0,
			Msg:  "修改失败",
			Data: nil,
		}
		t.ServeJSON()
		return
	} else {
		t.Data["json"] = Msg{
			Code: 200,
			Msg:  "修改成功",
			Data: tour,
		}
		t.ServeJSON()
	}
}

func (t *TourControllers) Del() {
	t.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	id, _ := t.GetInt("id")
	err := models.DB.Delete(&models.Tour{}, id).Error

	tour_redis_key := "tour_id" + strconv.Itoa(id)
	models.Rdb.Del(models.Ctx, tour_redis_key)

	if err != nil {
		t.Data["json"] = Msg{
			Code: 0,
			Msg:  "删除失败",
			Data: nil,
		}
		t.ServeJSON()
		return
	} else {
		t.Data["json"] = Msg{
			Code: 200,
			Msg:  "删除成功",
			Data: nil,
		}
		t.ServeJSON()
	}
}
